import React, { useContext, useEffect, useMemo } from 'react';
import Cls from 'classnames';
import { ConractControllerContext } from '../common';
import { DescriptionsType } from '@/components/DescriptionsType';
import ScrollBarLayout from '@/components/ScrollBarLayout';
import { getTaskDescConfigOA, renderTopBodyText } from '../mconfigOA';
import { ntkoQueryParams } from '../mconfig';
import { wordHandChildParam } from '@/utils';
import { NtkoPlugin } from '@/common/NtkoPluginType';
import moment from 'moment';

export const DetailInfoCenterViewApplyOA: React.FC = () => {
  /** 获取上下文 */
  const { styleFixCls, contractDetailInfo, curRes } = useContext(ConractControllerContext);

  /** 引入在线预览 */
  const ntkoBrowser = useMemo(() => {
    const { ntkoBrowser } = NtkoPlugin(() => {});
    return ntkoBrowser;
  }, []);

  /** 点击在线预览文件 */
  const onNtkoReadWord = (urlName: string, urlObj: Record<string, any>, onlyRead?: boolean) => {
    const { ntkoWordUrl } = ntkoQueryParams(urlObj, curRes, urlName, onlyRead);

    const childStr = wordHandChildParam(urlObj);

    const extensBool = ntkoBrowser.ExtensionInstalled();
    if (extensBool) {
      ntkoBrowser?.openWindow(ntkoWordUrl, false, '', childStr);
    }
  };

  /** 获取表单配置 */
  const { config, methods } = getTaskDescConfigOA({
    onNtkoReadWord,
  });

  useEffect(() => {
    if (contractDetailInfo?.id) {
      methods?.setFieldsValue({
        contractNameOA: contractDetailInfo?.contractName ? contractDetailInfo?.contractName : null,
        businessTypeNameOA: contractDetailInfo?.businessTypeName ? contractDetailInfo?.businessTypeName : null,
        ownDepartmentNameOA: contractDetailInfo?.ownDepartmentName ? contractDetailInfo?.ownDepartmentName : null,
        ownerNameOA: contractDetailInfo?.ownerName ? contractDetailInfo?.ownerName : null,
        busiDepartmentNameOA: contractDetailInfo?.busiDepartmentName ? contractDetailInfo?.busiDepartmentName : null,
        relateDepartmentNameOA: contractDetailInfo?.relateDepartmentName ? contractDetailInfo?.relateDepartmentName : null,
        sumInTaxOA: contractDetailInfo?.sumInTax ? contractDetailInfo?.sumInTax : null,
        projectNameOA: contractDetailInfo?.projectName ? contractDetailInfo?.projectName : null,
        procureRealNameOA: contractDetailInfo?.procureRealName ? contractDetailInfo?.procureRealName : null,
        bidTimeOA: contractDetailInfo?.bidTime ? moment(contractDetailInfo?.bidTime) : null,
        oppositeNameOA: contractDetailInfo?.oppositeName ? contractDetailInfo?.oppositeName : null,
        contractURLOA: contractDetailInfo?.contractURL ? contractDetailInfo?.contractURL : null,
      });
    }
  }, [contractDetailInfo?.id]);

  return (
    <>
      <div className="commonTabsBodyDom">
        <div className={Cls('tabsBodyInfoDom', `${styleFixCls}updateFormBox`)}>
          <ScrollBarLayout style={{ padding: '5px 100px' }}>
            <div className={`${styleFixCls}infoViewOA-centerBodyTitleBox`}>{renderTopBodyText(contractDetailInfo)}</div>
            <DescriptionsType config={config} titleType={true} />
          </ScrollBarLayout>
        </div>
      </div>
    </>
  );
};
